package com.app.web.front.model;

import java.util.List;

import com.app.util.SqlUtil;
import com.app.web.BaseModel;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
/**
 * 允许评论 allow_comment  (1:是, 0: 否)
 * @author Administrator
 *
 */
public class Blog extends BaseModel<Blog>{
	
	private static final long serialVersionUID = 613822483108524338L;
	public static final Blog dao = new Blog();
	
	public static int ALLOW_COMMENT_YES = 1;
	public static int ALLOW_COMMENT_NO = 0;
	
	
	public List<Record> findByCateId(int cateId){
		if(cateId > 0){
			return Db.find("select b.*, c.name as category_name from blog b left outer join category c on b.category_id = c.id where category_id = ?", cateId);
		}
		return Db.find("select * from blog");
	}
	
	public List<Record> findByParams(Record params){
		String cateId = params.get("cate_id","");
		String blogCateId = params.get("blog_cate_id", "");
		String userId = params.get("user_id","");
		String sql = "select b.*,c.name as category_name, "+
						"(select count(1) from favorite f where f.type_id = b.id and f.type = ?) as favorite_count, "+
						"(select count(1) from comment c where c.type_id = b.id and c.type = ?) as comment_count "+
						"from blog b "+
						"left outer join category c on b.category_id = c.id "+
						"left outer join blog_category bc on b.blog_cate_id = bc.id "+
						"left outer join user u on b.user_id = u.id ";
		SqlUtil sqlUtil = new SqlUtil();
		sqlUtil.addAndField("c", SqlUtil.EQUAL, "id", cateId);
		sqlUtil.addAndField("u", SqlUtil.EQUAL, "id", userId);
		
		//整体条件
		SqlUtil sub = new SqlUtil();
		sub.addOrField("bc", SqlUtil.EQUAL, "id", blogCateId);
		sub.addOrField("bc", SqlUtil.EQUAL, "pid", blogCateId);
		sqlUtil.addSub(sub);
		
		return Db.find(sql + sqlUtil.toWhere(), Favorite.TYPE_BLOG, Comment.TYPE_BLOG);
	}
	
	/**type 为博客类型的评论*/
	public int getFavoriteCount(int blogId){
		
		 Long count = Db.queryLong("select count(1) from blog b "+
					"inner join favorite f on b.id = f.type_id and type = ? "+
					"where f.type_id = ?", Favorite.TYPE_BLOG, blogId);
		
		 return count.intValue();
	}
	public boolean isFavorite(int blogId, int userId){
		
		Record record = Db.findFirst("select * from favorite where user_id = ? and type_id = ? and type=?", userId, blogId, Favorite.TYPE_BLOG);
		return record == null ? false : true;
	}
	
	public List<Record> getComments(int blogId){
		return Db.find("select c.*, u.user_name as sender_name,u.pic_url as sender_pic_path from comment c "+
							"left outer join user u on c.sender_id = u.id "+
							"where c.type_id = ? and c.type = ?", blogId, Comment.TYPE_BLOG);
	}

	public void updateViewCount(Record blog){
		int viewCount = blog.get("view_count",0);
		Db.update("update blog set view_count = ? where id = ?", ++viewCount, blog.getInt("id"));
	}
	
	public void deleteComments(int blogId) {
		Db.update("delete from comment where type_id = ? and type = ?", blogId, Comment.TYPE_BLOG);
	}

	public void deleteFavorite(int blogId) {
		// TODO Auto-generated method stub
		Db.update("delete from favorite where type_id = ? and type = ?", blogId, Comment.TYPE_BLOG);
	}
}
